﻿#ifndef CUSTOMTABLEMODEL_H
#define CUSTOMTABLEMODEL_H

#include <QAbstractTableModel>
#include <QVector>
#include <QList>

class CustomTableModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    explicit CustomTableModel(QObject *parent = nullptr);

    int rowCount(const QModelIndex &parent) const override;
    int columnCount(const QModelIndex &parent) const override;
    QVariant data(const QModelIndex &index, int role) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
    void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
    virtual int orginalIndex(int row) const;

    void setHeaders(const QList<QString> &headers);
    void setHeadersAndData(const QList<QString> &headers, const QVector<QVector<QString> > &data);
    bool isRowModified(int originalRow) const;
    QVector<QString> modelLine(int originalRow) const;
    QVector<QString> headers() const;
    QVector<QVector<QString> > modelData() const;
    Qt::ItemFlags flags(const QModelIndex &index) const override;

    void setCheckcolumn(const int& ncolumn);

public slots:
    void setModelData(const QVector<QVector<QString> > &data);

protected:
    QVector<QVector<QString> > m_v2d_data;
    QVector<QString> m_vec_header;
    QVector<bool> m_vec_rowModified;
    QHash<int, int> m_hash_originalToCurrent;
    int m_nRowCount = 0;
    int m_nColCount = 0;

    bool m_bSettingModelData = false;
    int m_nSortedColumn = 0;
    Qt::SortOrder m_orderSorted = Qt::AscendingOrder;
    bool m_bUseReset = true;
    int colNumberWithCheckBox = -1;
    QMap<int, Qt::CheckState> rowCheckStateMap;
};

#endif // CUSTOMTABLEMODEL_H
